<?php
namespace Modules\News\Http\Controllers;

use App\Http\Controllers\Controller;
use App\services\Wechatservice;
use Illuminate\Http\Request;
use Modules\News\Entities\NewsReply;
use Modules\News\Http\Requests\NewsReplyRequest;
use Modules\Wx\Entities\WxRole;

class NewsReplyController extends Controller
{
    //显示列表
    public function index()
    {
        $data = NewsReply::paginate(10);
        return view('news::news_reply.index', compact('data'));
    }

    //创建视图
    public function create(NewsReply $news_reply,Wechatservice $wechatservice)
    {
//        dd(123);
        $view = $wechatservice->keyshitu();
        return view('news::news_reply.create',compact('news_reply','view'));
    }

    //保存数据
    public function store(NewsReplyRequest $request,NewsReply $news_reply,Wechatservice $wechatservice)
    {
//        dd(123);
//        先将规则数据和关键词调用服务类进行保存
        $rule = $wechatservice->saverule();
//        再将图文数据保存到数据表里
        NewsReply::create([
           'data'=>$request->input('data'),
            'role_id'=>$rule['id']
        ]);
        return redirect('/news/news_reply')->with('success', '保存成功');
    }

    //显示记录
    public function show(NewsReply $field)
    {
        return view('news::news_reply.show', compact('field'));
    }

    //编辑视图
    public function edit(NewsReply $news_reply,Wechatservice $wechatservice)
    {
        $view = $wechatservice->keyshitu($news_reply['role_id']);
        return view('news::news_reply.edit', compact('news_reply','view'));
    }

    //更新数据
    public function update(NewsReplyRequest $request, NewsReply $news_reply,Wechatservice $wechatservice)
    {
//        $news_reply->update($request->all());
        $rule = $wechatservice->saverule($news_reply['role_id']);
//        对图文回复进行编辑
        $news_reply->data = $request->input('data');
        $news_reply->role_id = $rule['id'];
        $news_reply->save();
        return redirect('/news/news_reply')->with('success','更新成功');
    }

    //删除模型
    public function destroy(NewsReply $news_reply)
    {
       WxRole::destroy($news_reply['role_id']);
        $news_reply->delete();
        return redirect('news/news_reply')->with('success','删除成功');
    }
}
